package plus.xj17520.org.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import plus.xj17520.org.domain.Employee;
import plus.xj17520.org.mapper.EmployeeMapper;
import plus.xj17520.org.service.IEmplService;
import plus.xj17520.base.query.EmplQuery;
import plus.xj17520.base.result.JSONResult;

import java.util.List;

/**
 * @projectName: pethomeProject
 * @package: plus.xj17520.org.service.impl
 * @className: EmplServiceImpl
 * @author: XiaoJue
 * @description: TODO
 * @date: 2022/5/23 15:35
 * @version: 1.0
 */
@Service
public class EmplServiceImpl implements IEmplService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public JSONResult addOrUpdate(Employee employee) {
        if (employee.getId() != null)
            employeeMapper.updateByPrimaryKey(employee);
        else
            employeeMapper.insert(employee);
        return JSONResult.success();
    }

    @Override
    public JSONResult findOne(Long id) {
        return JSONResult.success(employeeMapper.selectByPrimaryKey(id));
    }

    @Override
    public JSONResult findAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return JSONResult.success(employees);
    }

    @Override
    public JSONResult delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        return JSONResult.success();
    }

    @Override
    public JSONResult emplPageList(EmplQuery emplQuery) {
//        Example example = new Example(Department.class);

        PageHelper.startPage(emplQuery.getCurrent(),emplQuery.getSize());
//        List<Employee> employees = employeeMapper.selectByExample(example);
        List<Employee> employees = employeeMapper.emplPageList(emplQuery);
        return JSONResult.success(new PageInfo<>(employees));
    }

    @Override
    public JSONResult batchDelete(EmplQuery emplQuery) {
        Long[] ids = emplQuery.getIds();
        for (Long id : ids)
            employeeMapper.deleteByPrimaryKey(id);
        return JSONResult.success();
    }

}
